<?php
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth, $fileName) 
{

  // open the directory

  $dir = opendir( $pathToImages );



  // loop through it, looking for any/all JPG files:

  //while (false !== ($fname = readdir( $dir ))) {

    // parse path for the extension

	$fname = $fileName;

    $info = pathinfo($pathToImages . $fname);

    // continue only if this is a JPEG image

	////////////////JPEG IMAGE///////////////

    if ( strtolower($info['extension']) == 'jpg' || strtolower($info['extension']) == 'jpeg' || strtolower($info['extension']) == 'jpe') 

    {

      //echo "Creating thumbnail for {$fname} <br />";



      // load image and get image size

      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );

	  	
      $width = imagesx( $img );

      $height = imagesy( $img );

	  $newheight = $thumbWidth;
	  $newwidth = $thumbWidth;

      // calculate thumbnail size

     /* $new_width = $thumbWidth;

      $new_height = floor( $height * ( $thumbWidth / $width ) );*/

		if($width >= $height) //if original width greter then or equal to original height then
		{	 
			if($width >= $newwidth) //if original width greter then or equal to thumbnail width then
			{
				$new_height = floor( $height * ( $thumbWidth / $width ) );
    			$new_height = $new_height;
				$new_width = $newwidth;  
			}
			else //if original width less then or equal to thumbnail width then
			{
				$thumbWidth = $width;
				$new_height = floor( $height * ( $thumbWidth / $width ) );
    			$new_height = $new_height;
				$new_width = $width;
			}
		}
		else //if original hieght greter then or equal to original width then
		{
			if($height >= $newheight) //if original height greter then or equal to thumbnail height then
			{
				$new_width = floor( $width * ($thumbWidth / $height));
				$new_width = $new_width;
		 		$new_height = $newwidth;
			}
			else //if original height less then or equal to thumbnail height then
			{
				$new_width = floor( $width * ($thumbWidth / $height));
				$new_width = $new_width;
		 		$new_height = $height;
			}
		}

      // create a new temporary image

      $tmp_img = imagecreatetruecolor( $new_width, $new_height );



      // copy and resize old image into new image 

      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );



      // save thumbnail into a file

      imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );

    }

	/////////GIF IMAGE///////////////

	else

	if ( strtolower($info['extension']) == 'gif' ) 

    {

      //echo "Creating thumbnail for {$fname} <br />";



      // load image and get image size

      $img = imagecreatefromgif( "{$pathToImages}{$fname}" );

      $width = imagesx( $img );

      $height = imagesy( $img );



      $newheight = $thumbHeight;
	  $newwidth = $thumbWidth;

      // calculate thumbnail size

     /* $new_width = $thumbWidth;

      $new_height = floor( $height * ( $thumbWidth / $width ) );*/

		if($width >= $height) //if original width greter then or equal to original height then
		{	 
			if($width >= $newwidth) //if original width greter then or equal to thumbnail width then
			{
				$new_height = floor( $height * ( $thumbWidth / $width ) );
    			$new_height = $new_height;
				$new_width = $newwidth;  
			}
			else //if original width less then or equal to thumbnail width then
			{
				$thumbWidth = $width;
				$new_height = floor( $height * ( $thumbWidth / $width ) );
    			$new_height = $new_height;
				$new_width = $width;
			}
		}
		else //if original hieght greter then or equal to original width then
		{
			if($height >= $newheight) //if original height greter then or equal to thumbnail height then
			{
				$new_width = floor( $width * ($thumbWidth / $height));
				$new_width = $new_width;
		 		$new_height = $newwidth;
			}
			else //if original height less then or equal to thumbnail height then
			{
				$new_width = floor( $width * ($thumbWidth / $height));
				$new_width = $new_width;
		 		$new_height = $height;
			}
		}


      // create a new temporary image

      $tmp_img = imagecreatetruecolor( $new_width, $new_height );



      // copy and resize old image into new image 

      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );



      // save thumbnail into a file

      imagegif( $tmp_img, "{$pathToThumbs}{$fname}" );

    }

	else

	if ( strtolower($info['extension']) == 'png' ) 

    {

      //echo "Creating thumbnail for {$fname} <br />";



      // load image and get image size

      $img = imagecreatefrompng( "{$pathToImages}{$fname}" );

      $width = imagesx( $img );

      $height = imagesy( $img );


	  $newheight = $thumbHeight;
	  $newwidth = $thumbWidth;

      // calculate thumbnail size

     /* $new_width = $thumbWidth;

      $new_height = floor( $height * ( $thumbWidth / $width ) );*/

		if($width >= $height) //if original width greter then or equal to original height then
		{	 
			if($width >= $newwidth) //if original width greter then or equal to thumbnail width then
			{
				$new_height = floor( $height * ( $thumbWidth / $width ) );
    			$new_height = $new_height;
				$new_width = $newwidth;  
			}
			else //if original width less then or equal to thumbnail width then
			{
				$thumbWidth = $width;
				$new_height = floor( $height * ( $thumbWidth / $width ) );
    			$new_height = $new_height;
				$new_width = $width;
			}
		}
		else //if original hieght greter then or equal to original width then
		{
			if($height >= $newheight) //if original height greter then or equal to thumbnail height then
			{
				$new_width = floor( $width * ($thumbWidth / $height));
				$new_width = $new_width;
		 		$new_height = $newwidth;
			}
			else //if original height less then or equal to thumbnail height then
			{
				$new_width = floor( $width * ($thumbWidth / $height));
				$new_width = $new_width;
		 		$new_height = $height;
			}
		}



      // create a new temporary image

      $tmp_img = imagecreatetruecolor( $new_width, $new_height );



      // copy and resize old image into new image 

      imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );



      // save thumbnail into a file

      imagepng( $tmp_img, "{$pathToThumbs}{$fname}" );

    }

 // }

  // close the directory

  closedir( $dir );

}

?>